package org.westboy.common.base;

import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class MediaService {

   private static MediaPlayer mediaPlayer;
   private static Media  media;
   private static Boolean atEndOfMedia = false;
    private static Boolean repeat = true;

   @PostConstruct
   private void init(){
       media =  new Media("http://www.jyg.cn/image/audio.mp3");
       mediaPlayer = new MediaPlayer(this.media);
       mediaPlayer.setCycleCount(-1);
       mediaPlayer.setOnEndOfMedia(() -> {
           atEndOfMedia = true;
       });

   }

   public void play(){
       MediaPlayer.Status status = status();
       System.out.println(status);

       if (status == MediaPlayer.Status.UNKNOWN  || status== MediaPlayer.Status.HALTED)
       {
           return;
       }

       if ( status == MediaPlayer.Status.PAUSED
               || status == MediaPlayer.Status.READY
               || status == MediaPlayer.Status.STOPPED)
       {
           if (atEndOfMedia) {
               mediaPlayer.seek(mediaPlayer.getStartTime());
               atEndOfMedia = false;
           }
           mediaPlayer.play();
       } else {
           stop();
       }

   }

    public void stop(){
        this.mediaPlayer.stop();
    }

   public MediaPlayer.Status status(){
       return this.mediaPlayer.getStatus();
   }

}
